package cn.itsource.controller;

import cn.itsource.annotation.PreAuthorize;
import cn.itsource.domain.Logininfo;
import cn.itsource.domain.Logininfo;
import cn.itsource.query.LogininfoQuery;
import cn.itsource.service.ILogininfoService;
import cn.itsource.util.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
@Api(tags = "登录接口")
public class LogininfoinfoController {
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 增加/修改
     * @param
     * @return
     */
    @ApiOperation(value = "增加/修改")
    @PutMapping
    @PreAuthorize(sn="logininfo:add", name = "登录增加/修改",descs = "登录增加/修改")
    public R addORupdate(@RequestBody Logininfo logininfo){
        try {
            if (logininfo.getId() == null){
                logininfoService.add(logininfo);
            }else {
                logininfoService.update(logininfo);
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(sn="logininfo:del", name = "登录删除",descs = "登录删除")
    public R del(@PathVariable("id") Long id){
        try {
            logininfoService.del(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @PatchMapping
    @PreAuthorize(sn="logininfo:delList", name = "登录批量删除",descs = "登录批量删除")
    public R delList(@RequestBody List<Long> ids){
        try {
            logininfoService.delList(ids);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }
    /**
     * 登录接口
     * @param
     * @return
     */
    @ApiOperation(value = "登录接口")
    @GetMapping("/getOne")
    public R getOne(@RequestBody Logininfo logininfo){
        try {
            return R.ok(logininfoService.get(logininfo));
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    @PreAuthorize(sn="logininfo:getAll", name = "登录查询所有",descs = "登录查询所有")
    public R getAll(){
        try {
            return R.ok(logininfoService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(sn="logininfo:pageQuery", name = "登录分页查询",descs = "登录分页查询")
    public PageInfo<Logininfo> pageQuery(@RequestBody LogininfoQuery query){
        return logininfoService.pageQuery(query);
    }
}
